﻿/*
* Copyright (c) 2022,
* All rights reserved.
*
* 文件名称：petmusic.h
* 创建日期：2022/3/12
* 摘 要：宠物音乐类接口声明
*
* 当前版本：1.0
* 作 者：mason
* 完成日期：
*/

#ifndef PETMUSIC_H
#define PETMUSIC_H

#include <QObject>
#include <QNetworkAccessManager>
#include <QNetworkRequest>
#include <QNetworkReply>
#include <QVector>
#include <QJsonDocument>
#include <QJsonParseError>
#include <QFile>
#include <QJsonObject>
#include <QDebug>
#include <QJsonArray>
#include <QMediaPlayer>
#include <QSqlDatabase>
#include <QDebug>
#include <QMessageBox>
#include <QSqlError>
#include <QSqlQuery>    //sql语句
#include <QVariantList>
#define KUWO_SORT_URL "http://search.kuwo.cn/r.s?client=kt&all=%1&pn=0&rn=10&ver=kwplayer_ar_9.0.7.0_138.apk&vipver=1&ft=music&cluster=0&strategy=2012&encoding=utf8&rformat=json&vermerge=1&mobi=1"
#define KUWO_URL_GET    "http://antiserver.kuwo.cn/anti.s?type=convert_url&rid=%1&format=mp3&response=url"

typedef struct _SONG_ {
    QString songname;   //歌曲名
    QString fartist;    //歌手名
    QString musicrid;   //id
    QString playurl;    //播放url
}SONG;

#define STR_SONGNAME "SONGNAME"
#define STR_MUSICRID "MUSICRID"
#define STR_FARTIST "FARTIST"

class PetMusic : public QObject
{
    Q_OBJECT
public:
    explicit PetMusic(QObject *parent = nullptr);
    ~PetMusic();
    void sortMusic(QString text);   //搜索音乐，结果会保存至sort_result_list
    void playMusic(SONG song);  //播放音乐
    void playMusic();  //播放音乐
    void getMusicListFromDb();
    void insertMusicToDb(SONG song);
    void stopMusic();
    QString getCurrentMusicInfo();



private:
    QMediaPlayer *player;
    QNetworkAccessManager* m_sort;  //搜索request
    QNetworkAccessManager* m_music_url; //播放url请求
    QNetworkAccessManager* m_music_dowland; //下载url请求
    QVector<SONG> sort_result_list; //搜索结果
    void analyzeSortResult(QByteArray bytes);   //处理搜索结果，保存至sort_result_list

    SONG song_now;  //当前播放
    int song_index;
    bool is_add;
    QSqlDatabase db;

signals:

public slots:
     void slotSortFinished(QNetworkReply *);
     void slotGetUrlFinished(QNetworkReply *);
     void slotDowlandFinished(QNetworkReply *);
     void slotPlayStop(QMediaPlayer::State);
     void musicNext();
};

#endif // PETMUSIC_H
